#include <math.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

static int l_sin (lua_State *L) 
{
	const char *sour = lua_tostring(L,1);
	const char *area = lua_tostring(L,2);
	char *buf;
	char *buffernumber;
	struct in_addr addr1,addr2;	
	if (!inet_aton(sour,&addr1))
	{
		lua_pushnumber(L,1);
		return 1;
	}
	buffernumber = (char *) malloc(30);
	buf = strstr(area,"/");
	strcpy(buffernumber,buf+1);   
	int ofsize,ipaddr,iparea;
	openlog("testsyslog", LOG_CONS | LOG_PID, 0);   
	syslog(LOG_USER | LOG_DEBUG,"HElloworld");
	closelog();
	ofsize = atoi(buffernumber);
	if (ofsize == 0 ) 
	{
		free(buffernumber);
		buffernumber = NULL;
		lua_pushnumber(L,2);
		return 2;
	}
	memset(buffernumber,'\0',30);
	strncpy(buffernumber,area,abs(area-buf));	
	if (!inet_aton(buffernumber,&addr2))
	{

		free(buffernumber);
		buffernumber = NULL;
		lua_pushnumber(L,3);
		return 3;
	}
	ofsize = 32 - ofsize;
	ipaddr = addr1.s_addr << ofsize ;
	iparea = addr2.s_addr << ofsize ;
	if (ipaddr == iparea )
	{

		free(buffernumber);
		buffernumber = NULL;
		lua_pushnumber(L,0);
		return 0 ;
	}
	else {

		free(buffernumber);
		buffernumber = NULL;
		lua_pushnumber(L,4);
		return 4;
	}/* number of results */
}

static const struct luaL_reg mylib [] = {
{"lsin", l_sin},
{NULL, NULL} /* 必须以NULL结尾 */
};

int luaopen_mylib (lua_State *L) 
{
luaL_openlib(L, "mylib1", mylib, 0);
return 1;
}
